﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TomNet.IMSC.Web.Models.StaffApiParams;
using TomNet.Web.WebApi;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Core.Contracts.Decoration;
using TomNet.IMSC.Model.DbModels.Decoration;
using TomNet.Web.Mvc.UI;
using TomNet.IMSC.Web.Filters;

namespace TomNet.IMSC.Web.Controllers.StaffAPI
{
    [ApiAuthentication]
    public class StaffDecorationCompanyListController : BaseApiController
    {
        public IDecorationCompanyContract DecorationCompanyContract { get; set; }
        public IDecorationCompanyStatisticsContract DecorationCompanyStatisticsContract { get; set; }
        public IDecorationCompanyStoreContract IDecorationCompanyStoreContract { get; set; }
        [Description("API-装修公司列表")]
        public IHttpActionResult ttGetDecorationCompanyList([FromBody]DecorationCompanyParamsModel entity)
        {

            var query = DecorationCompanyContract.Entities.Where(m => m.IsDeleted == false);

            //条件搜索
            if (!string.IsNullOrWhiteSpace(entity.KeyWord))
            {
                query = query.Where(m => m.Name.Contains(entity.KeyWord));
            }

            var list = (from m in query
                        join n in DecorationCompanyStatisticsContract.Entities
                        on m.Id equals n.DecorationCompanyID into temp
                        from tt in temp.DefaultIfEmpty()
                        let address = (from s in IDecorationCompanyStoreContract.Entities where s.DecorationCompanyID == m.Id select s.Address).FirstOrDefault()
                        select new DecorationCompanyListModel
                        {
                            Id = m.Id,
                            Name = m.Name,
                            Address = (address == null ? "" : address),
                            DecorationCompanyInfo = m.DecorationCompanyInfo,
                            CreatedTime = m.CreatedTime,

                            FilingNumber_Total = (tt == null) ? 0 : tt.FilingNumber_Total,
                            FilingNumber_30Day = (tt == null) ? 0 : tt.FilingNumber_30Day,
                            FilingNumber_7Day = (tt == null) ? 0 : tt.FilingNumber_7Day,

                            SuccessNumber_Total = (tt == null) ? 0 : tt.SuccessNumber_Total,
                            SuccessNumber_30Day = (tt == null) ? 0 : tt.SuccessNumber_30Day,
                            SuccessNumber_7Day = (tt == null) ? 0 : tt.SuccessNumber_7Day


                        });

            //数据总数
            var total = list.Count();

            //默认给一个排序的字段
            list = list.OrderByDescending(m => m.CreatedTime);

            //分页(假如total == 0，则取消分页查询，提高性能)
            list = total > 0 ? list.Skip((entity.PageIndex - 1) * entity.PageSize).Take(entity.PageSize)
                : Enumerable.Empty<DecorationCompanyListModel>().AsQueryable();// null;


            var result = new AjaxResult("成功", AjaxResultType.Success, list.ToList());
            return Token(result, "");

        }
    }
}